//
//  WeChatUtil.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/22.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "WeChatUtil.h"

@implementation WeChatUtil

+(void)sendAuthRequest
{
    //构造SendAuthReq结构体
    SendAuthReq* req =[[SendAuthReq alloc ] init];
    req.openID  = WXAPP_ID;
    req.scope = @"snsapi_userinfo" ;
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];
}

/**
 *  获取登录授权后获得的参数
 *
 *  @param mResp
 */
+(void)getWxResqParams:(BaseResp *)mResp{
    SendAuthResp *temp = (SendAuthResp*)mResp;
    NSString *code = temp.code;
    NSString *state = temp.state;
    if([Utils isBlankString:code] && [Utils isBlankString:state]){//获取授权失败
        
    }else{
        [self getWXToken:code];
    }
    JXLog(@"code%@----state%@-----",code,state);
}

/**
 * 获取access_token信息
 */
+(void)getWXToken:(NSString *)code{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:WXAPP_ID forKey:@"appid"];
    [params setObject:WXAPP_SECRET forKey:@"secret"];
    [params setObject:code forKey:@"code"];
    [params setObject:@"authorization_code" forKey:@"grant_type"];
    [WeChatDataTool getWXToken:params success:^(WeChatLoginRes *result) {
        if([Utils isBlankString:result.errmsg]){//获取token成功
            [UserDefaultsHelper setObject:result.openid forKey:WX_OPENID];
            [UserDefaultsHelper setObject:result.access_token forKey:WX_ACCESS_TOKEN];
            //检查token是否可用
            [self checkToken:result];
        }else{
            //   TODO    [DialogUtils showTipsWithView:self String:result.errmsg];
        }
    } failure:^(NSError *error) {
        
    }];
}

/**
 *  检测token是否过期
 */
+(void)checkToken:(WeChatLoginRes *)result{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:result.access_token forKey:@"access_token"];
    [params setObject:result.openid forKey:@"openid"];
    [WeChatDataTool checkToken:params success:^(WeChatLoginRes *result) {
        if(result.errcode == 0){
            [self getUserInfo];
            JXLog(@"微信登录检测token 返回值 %@",result.errmsg);
        }else{
            //TODO
        }
    } failure:^(NSError *error) {
        
    }];
}

/**
 * 获取微信用户个人信息
 */
+(void)getUserInfo{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[UserDefaultsHelper stringForKey:WX_ACCESS_TOKEN] forKey:@"access_token"];
    [params setObject:[UserDefaultsHelper stringForKey:WX_OPENID] forKey:@"openid"];
    [WeChatDataTool getUserinfo:params success:^(WeChatUserinfoRes *result) {
        if(result.errmsg){//获取用户信息成功
            //TODO 提示
        }else{
            // 保存用户昵称，性别，头像地址,唯一标示
            [UserDefaultsHelper setObject:result.openid forKey:WX_OPENID];
            [result setClientType:@"3"];
            //TODO 把获取到的信息传输到登录回调请求服务器进行第三方登录
            [[NSNotificationCenter defaultCenter] postNotificationName:@"oauth_login" object:result];
        }
    } failure:^(NSError *error) {
        
    }];
}


@end
